//
//  BHCircleContentTextCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/12.
//

import UIKit

class BHCircleContentTextCell: UICollectionViewCell {

    @IBOutlet weak var place_label: UILabel!
    
    @IBOutlet weak var text_view: UITextView!
    
    @IBOutlet weak var numberCount_label: UILabel!
    
    var textViewBegainEditingBlock:(() -> Void)?
    var textViewDidEndEditingBlock:((String) -> Void)?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.text_view.delegate = self
        
        self.place_label.addViewAction {[weak self] (_) in
            self?.text_view.becomeFirstResponder()
        }
    }

}

extension BHCircleContentTextCell: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.textViewBegainEditingBlock?()
    }
    func textViewDidChange(_ textView: UITextView) {
        self.place_label.isHidden = !textView.text.isEmpty
        if textView.text.count > 500 {
            textView.text = textView.text.id_subString(to: 500)
        }
        self.numberCount_label.text = "\(textView.text.count)/500"
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        self.textViewDidEndEditingBlock?(textView.text ?? "")
    }
}
